package com.hhh.springai_test.controller;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.hhh.springai_test.Client.TongYiChatClient;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.function.Function;

@RestController
@CrossOrigin
public class TongYIController {

    @Resource
    private TongYiChatClient tongYiChatClient;

    public record Request(
            @JsonProperty(required = true)
            @JsonPropertyDescription("举报的名字是") String name) {

    }

    @GetMapping(value = "/ai/generateStreamAsString", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<String> generateStreamAsString(@RequestParam(value = "message") String message) {
        Flux<String> content = tongYiChatClient.getTongYiChatClient().prompt()
                .user(message)
                .function("report", "举报", new Function<TongYIController.Request, Flux<String>>() {
                    @Override
                    public Flux<String> apply(TongYIController.Request request) {
                        System.out.println("举报名字是" + request.name);
                        return Flux.just("星轨");
                    }
                })
                .functions()
                .stream()
                .content();
        return content;
    }
}
